package com.ly.nethub.bean

/**
 * TCP连接用
 * @param ip ip地址 192.168.1.1
 * @param port 端口号 8080
 */
data class IPInfo(val ip: String, val port: Int) {

    //是否是合法的ip
    val isLegalIP: Boolean get() = IPRegex.matches(ip)

    //是否是合法端口
    val isLegalPort: Boolean get() = PortRegex.matches(port.toString())

    override fun toString(): String {
        return "${ip}:${port}"
    }

    companion object {
        val IPRegex =
            Regex("^((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}\$")

        val PortRegex = Regex("^([1-9]\\d{0,3})|^0")
    }
}
